package com.imay.live.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.imay.live.R;

/**
 * Created by chan on 2017/4/2.
 */

public class ToastUtil {
    private static Toast TOAST;
    private static final String TAG = "ToastUtil";


    //短时间吐司
    public static void show(Context context, int resourceID) {
        show(context, resourceID, Toast.LENGTH_SHORT);
    }

    //短时间吐司
    public static void show(Context context, String text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    //自定义时长吐司
    public static void show(Context context, Integer resourceID, int duration) {
        String text = context.getResources().getString(resourceID);// 用于显示的文字
        show(context, text, duration);
    }

    //自定义时长吐司
    public static void show(@NonNull final Context context, @NonNull final String text, final int duration) {
        if (TOAST == null) {
            TOAST = Toast.makeText(context.getApplicationContext(), text, duration);
        } else {
            TOAST.setText(text);
            TOAST.setDuration(duration);
        }
        TOAST.setGravity(Gravity.CENTER, 0, 0);
        TOAST.show();
    }

    /**
     * @param context
     * @param text
     * @param duration
     */
    public static void showImg(final Context context, String text, int imgRid, int duration) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
        ImageView mToastImg = (ImageView) view.findViewById(R.id.toast_img);
        TextView mTextView = (TextView) view.findViewById(R.id.toast_text);
        Toast toast = Toast.makeText(context, text, duration);
        mToastImg.setImageResource(imgRid);
        mTextView.setText(text);
        toast.setDuration(duration);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
